//: src/com/silvtar/rsync/SSRsync.java
/* 
 * Created by 朴晟宁 <piaosn@gmail.com> on 5/25/2013. 
 * Copyright (c) 2013 朴晟宁. All rights reserved.
 * 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program;
 * 
 * *********************************************************************
 * 
 * Modify History:
 * Date        Version    Description
 * 5/25/2013   0.1        Create the class. 
 */
package com.silvtar.rsync;

import com.silvtar.io.StdOut;
import com.silvtar.util.Bin;

/**
 * rsync打包类 本类将rsync打包使它更加易于使用
 * 
 * @author 朴晟宁/Silvtar
 * @author piaosn@gmail.com
 * @version 0.1
 */
public class SSRsync {

    private int     port;
    private String  ipAddr;
    private String  userName;
    private boolean compress;
    private boolean verbose;

    private String  path;

    public void configRsync(String userName, String ipAddr, int port,
            boolean compress, boolean verbose) {
        this.userName = userName;
        this.ipAddr = ipAddr;
        this.port = port;
        this.compress = compress;
        this.verbose = verbose;
    }

    public void configRsync(String userName, String ipAddr, int port) {
        this.userName = userName;
        this.ipAddr = ipAddr;
        this.port = port;
        this.compress = true;
        this.verbose = true;
    }

    /**
     * 开始同步。
     * 
     * @param path
     *            本地路径
     * @param moduleName
     *            远程模块名
     */
    public void startRsync(String path, String moduleName) {
        // 生成基础命令
        String cmd_basic = "--password-file=./assets/rsync/rsync.ps -aru";
        if (compress)
            cmd_basic += "z";
        if (verbose)
            cmd_basic += "v";
        cmd_basic = cmd_basic + " --port=" + port;

        // 生成服务器地址
        String cmd_server = userName + "@" + ipAddr + "::" + moduleName;

        // 生成两个同步方向的命令
        String cmd1 = cmd_basic + " " + path + " " + cmd_server;
        String cmd2 = cmd_basic + " " + cmd_server + " " + path;

        // 运行命令
        StdOut.printLog("\n 上传：" + cmd1 + "\n");
        Bin.rsync(cmd1);

        StdOut.printLog("\n 下载：" + cmd2 + "\n");
        Bin.rsync(cmd2);
    }
}
